package com.bootdo.common.utils;

import org.apache.commons.io.FileUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.util.UUID;

public class FileUtil {

	public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		FileOutputStream out = new FileOutputStream(filePath + fileName);
		out.write(file);
		out.flush();
		out.close();
	}

	public static boolean deleteFile(String fileName) {
		File file = new File(fileName);
		// 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
		if (file.exists() && file.isFile()) {
			if (file.delete()) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	public static String renameToUUID(String fileName) {
		return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
	}

	public static void uploadFileBuffer(InputStream in, String filePath, String fileName) throws Exception {
		//	B/ms 假设每秒上传10M 10000kB/s==10B/ms   1M/s >1B/ms

		int speed = 1;
		Long startTime = System.currentTimeMillis();
		long start = startTime;
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		BufferedInputStream bis = new BufferedInputStream(in);
		BufferedOutputStream os=new BufferedOutputStream(new FileOutputStream(filePath + fileName));
		byte[] b = new byte[1024];

		long readed = 0L;
		int length;
		while((length = bis.read(b))!=-1){
			os.write(b,0, length);
			//当前花费的时间
			readed+=length;
			if(readed/1000>1){
				System.out.println("长度：length"+length);
				System.out.println("已经读取了"+readed/1000+"M");
			}
			System.out.println("已经读取了");
			//本来应该花费的时间
			long costMs = readed/speed;
			long costedMs = System.currentTimeMillis() - startTime ;
			System.out.println();
			long sleepTime = costMs - costedMs;
			System.out.println("休眠一下："+sleepTime/1000+"秒");
			if ((sleepTime) > 0) {
				Thread.sleep(sleepTime);
			}
		}


//		try {
//			long count = 0;
//			int j;
//			while ((j = bis.read(b)) != -1) {
//				if(j!=1024){
//					System.out.println("怎么会死循环？");
//				}
//
//				if (count + j > speed) {
//					int need = (int) (speed - count);
//					// 剩下的数
//					int left = (int) (j + count - speed);
//					byte[] temp = new byte[need];
//					byte[] leftTemp = new byte[left];
//					System.arraycopy(b, 0, temp, 0, need);
//					System.arraycopy(b, need, leftTemp, 0, left);
//					os.write(temp);
//					os.flush();
//					long endTime = System.currentTimeMillis();
//					long sleepTime = startTime + 1000 - endTime;
//					if (sleepTime > 0) {
//						Thread.sleep(sleepTime);
//					}
//
//					startTime = System.currentTimeMillis();
//					count = 0;
//					os.write(leftTemp);
//					os.flush();
//					count += left;
//					continue;
//				}
//
//				if (count + j < speed) {
//					count += j;
//					byte[] temp = new byte[j];
//					System.arraycopy(b, 0, temp, 0, j);
//					os.write(temp);
//					os.flush();
//					continue;
//				}
//				if (count + j == speed) {
//					byte[] temp = new byte[j];
//					System.arraycopy(b, 0, temp, 0, j);
//					os.write(temp);
//					os.flush();
//					long endTime = System.currentTimeMillis();
//					long sleepTime = startTime + 1000 - endTime;
//					if (sleepTime > 0) {
//						Thread.sleep(sleepTime);
//					}
//					// 重置计数器
//					startTime = System.currentTimeMillis();
//					count = 0;
//					continue;
//
//				}
//			}
//
//		} catch (IOException e1) {
//			System.out.println("流传输异常");
//		} catch (InterruptedException e) {
//			System.out.println("线程断了。");
//		}
		os.flush();
		os.close();
		Long completeTime = System.currentTimeMillis();
		try {
			long costTime = (completeTime-start)/1000;
			System.out.println("下载花费时间："+costTime+"s");
			System.out.println("平均速度："+bis.available()/(costTime*1000)+" kb/s");
		}catch (Exception e ){
		}



	}

	private Long download(HttpServletResponse response, InputStream in, Long speed) throws Exception{
		Long startTime = System.currentTimeMillis();
		long start = startTime;
		byte[] b = null;
		b = new byte[1024];
		try {
			OutputStream os = null;
			os = response.getOutputStream();
			long count = 0;
			int j;
			while ((j = in.read(b)) != -1) {

				if (count + j > speed) {
					int need = (int) (speed - count);
					// 剩下的数
					int left = (int) (j + count - speed);
					byte[] temp = new byte[need];
					byte[] leftTemp = new byte[left];
					System.arraycopy(b, 0, temp, 0, need);
					System.arraycopy(b, need, leftTemp, 0, left);
					os.write(temp);
					os.flush();
					long endTime = System.currentTimeMillis();
					long sleepTime = startTime + 1000 - endTime;
					if (sleepTime > 0) {
						Thread.sleep(sleepTime);
					}

					startTime = System.currentTimeMillis();
					count = 0;
					os.write(leftTemp);
					os.flush();
					count += left;
					continue;
				}

				if (count + j < speed) {
					count += j;
					byte[] temp = new byte[j];
					System.arraycopy(b, 0, temp, 0, j);
					os.write(temp);
					os.flush();
					continue;
				}
				if (count + j == speed) {
					byte[] temp = new byte[j];
					System.arraycopy(b, 0, temp, 0, j);
					os.write(temp);
					os.flush();
					long endTime = System.currentTimeMillis();
					long sleepTime = startTime + 1000 - endTime;
					if (sleepTime > 0) {
						Thread.sleep(sleepTime);
					}
					// 重置计数器
					startTime = System.currentTimeMillis();
					count = 0;
					continue;

				}
			}

		} catch (IOException e1) {
		} catch (InterruptedException e) {
		} finally {
			try {
				in.close();
			} catch (IOException e) {
			}
		}
		Long completeTime = System.currentTimeMillis();
		try {
			System.out.println("下载花费时间："+(completeTime-start)/1000+"s");
		}catch (Exception e ){
		}

		return startTime;
	}


	private Long writeResponse(HttpServletResponse response, InputStream stream, Long speed, Long startTime, MessageDigest md5) throws Exception{
		byte[] b = null;
		b = new byte[1024];
		try {
			OutputStream os = null;
			os = response.getOutputStream();
			long count = 0;
			int j;
			while ((j = stream.read(b)) != -1) {

				if (count + j > speed) {
					int need = (int) (speed - count);
					// 剩下的数
					int left = (int) (j + count - speed);
					byte[] temp = new byte[need];
					byte[] leftTemp = new byte[left];
					System.arraycopy(b, 0, temp, 0, need);
					System.arraycopy(b, need, leftTemp, 0, left);
					os.write(temp);
					md5.update(temp);
					os.flush();
					long endTime = System.currentTimeMillis();
					long sleepTime = startTime + 1000 - endTime;
					if (sleepTime > 0) {
						Thread.sleep(sleepTime);
					}

					startTime = System.currentTimeMillis();
					count = 0;
					os.write(leftTemp);
					md5.update(leftTemp);
					os.flush();
					count += left;
					continue;
				}

				if (count + j < speed) {
					count += j;
					byte[] temp = new byte[j];
					System.arraycopy(b, 0, temp, 0, j);
					os.write(temp);
					md5.update(b);
					os.flush();
					continue;
				}
				if (count + j == speed) {
					byte[] temp = new byte[j];
					System.arraycopy(b, 0, temp, 0, j);
					os.write(temp);
					md5.update(b);
					os.flush();
					long endTime = System.currentTimeMillis();
					long sleepTime = startTime + 1000 - endTime;
					if (sleepTime > 0) {
						Thread.sleep(sleepTime);
					}
					// 重置计数器
					startTime = System.currentTimeMillis();
					count = 0;
					continue;

				}
			}

		} catch (IOException e1) {
		} catch (InterruptedException e) {
		} finally {
			try {
				stream.close();
			} catch (IOException e) {
			}
		}

		return startTime;
	}

	}
